﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using Target = KC.Business.Text.Parsers;
using C = KC.Constants;

namespace KC.Tests.Business.Text.Parsers
{
	[TestClass]
	public class DataTranslator : Target.DataTranslator
	{
		[TestMethod]
		[TestCategory("Unit")]
		public void TranslateTableTest()
		{
			using (DataTable 
					dtMatrix = BuildTranslateMatrix(), 
					dtData = BuildDataTable(), 
					dtTranslated = Target.DataTranslator.TranslateTable(dtData, dtMatrix)) {
				// Ensure non-matching-named columns are not translated
				for (int i = 0; i < dtMatrix.Rows.Count; i++) {
					Assert.AreEqual(dtData.Rows[i][0].ToString(), dtTranslated.Rows[i][0].ToString());
					Assert.AreEqual(dtData.Rows[i][3].ToString(), dtTranslated.Rows[i][3].ToString());
					Assert.AreEqual(dtData.Rows[i][4].ToString(), dtTranslated.Rows[i][4].ToString()); }
				
				// Ensure translation columns are translated
				Assert.AreEqual(dtTranslated.Rows[0][1].ToString(), "---myNewValue1a---");
				Assert.AreEqual(dtTranslated.Rows[1][1].ToString(), "---myNewValue1b---");
				Assert.AreEqual(dtTranslated.Rows[2][1].ToString(), "---myOldValue2---");

				Assert.AreEqual(dtTranslated.Rows[0][2].ToString(), "---myOldValue1a---");
				Assert.AreEqual(dtTranslated.Rows[1][2].ToString(), "---myOldValue1b---");
				Assert.AreEqual(dtTranslated.Rows[2][2].ToString(), "---myNewValue2---"); }

				#region How do we know this is right?
				// Translate Matrix
				//	ColumnName	OldValue		NewValue
				// 	Col1		myOldValue1a	myNewValue1a
				//	Col1		myOldValue1b	myNewValue1b
				//	Col2		myOldValue2		myNewValue2

				// From
				//	Normal1				Col1				Col2				Normal2				Normal3
				//	---myOldValue1a---	---myOldValue1a---	---myOldValue1a---	---myOldValue1a---	---myOldValue1a---
				//	---myOldvalue1b---	---myOldvalue1b---	---myOldvalue1b---	---myOldvalue1b---	---myOldvalue1b---
				//	---myOldValue2---	---myOldValue2---	---myOldValue2---	---myOldValue2---	---myOldValue2---

				// To
				//	Normal1				Col1				Col2				Normal2				Normal3
				//	---myOldValue1a---	---myNewValue1a---	---myOldValue1a---	---myOldValue1a---	---myOldValue1a---
				//	---myOldvalue1b---	---myNewvalue1b---	---myOldvalue1b---	---myOldvalue1b---	---myOldvalue1b---
				//	---myOldValue2---	---myOldValue2---	---myNewValue2---	---myOldValue2---	---myOldValue2---

				// Diff
				//	Normal1				Col1				Col2				Normal2				Normal3
				//	*					---myNewValue1a---	---myOldValue1a---	*					*
				//	*					---myNewvalue1b---	---myOldvalue1b---	*					*
				//	*					---myOldValue2---	---myNewValue2---	*					*
				#endregion asenine comment
		}
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		[TestCategory("Unit")]
		public void TranslateTableNullTest1()
		{
			using (DataTable
					dtMatrix = null,
					dtData = BuildDataTable(),
					dtTranslated = Target.DataTranslator.TranslateTable(dtData, dtMatrix)) { }
		}
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		[TestCategory("Unit")]
		public void TranslateTableNullTest2()
		{
			using (DataTable
					dtMatrix = BuildTranslateMatrix(),
					dtData = null,
					dtTranslated = Target.DataTranslator.TranslateTable(dtData, dtMatrix)) { }
		}

		[TestMethod]
		[TestCategory("Unit")]
		public void TranslateCellTest()
		{
			using (DataTable dtMatrix = BuildTranslateMatrix()) {
				string result = Target.DataTranslator.TranslateCell(dtMatrix, "---myOldValue1b---", "Col1");
				Assert.AreEqual(result, "---myNewValue1b---");
			}
		}
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		[TestCategory("Unit")]
		public void TranslateCellNullTest1()
		{
			Target.DataTranslator.TranslateCell(null, "---myOldValue1b---", "Col1");
		}
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		[TestCategory("Unit")]
		public void TranslateCellNullTest2()
		{
			using (DataTable dtMatrix = BuildTranslateMatrix())
				Target.DataTranslator.TranslateCell(dtMatrix, null, "Col1");
		}

		private static DataTable BuildTranslateMatrix()
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("ColumnName", typeof(string));
			dt.Columns.Add("OldValue", typeof(string));
			dt.Columns.Add("NewValue", typeof(string));

			DataRow dr = dt.NewRow();
			dr[0] = "Col1";
			dr[1] = "myOldValue1a";
			dr[2] = "myNewValue1a";
			dt.Rows.Add(dr);

			dr = dt.NewRow();
			dr[0] = "Col1";
			dr[1] = "myOldValue1b";
			dr[2] = "myNewValue1b";
			dt.Rows.Add(dr);

			dr = dt.NewRow();
			dr[0] = "Col2";
			dr[1] = "myOldValue2";
			dr[2] = "myNewValue2";
			dt.Rows.Add(dr);

			return dt;
		}
		private static DataTable BuildDataTable()
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("Normal1", typeof(string));
			dt.Columns.Add("Col1", typeof(string));
			dt.Columns.Add("Col2", typeof(string));
			dt.Columns.Add("Normal2", typeof(string));
			dt.Columns.Add("Normal3", typeof(string));

			DataRow dr = dt.NewRow();
			dr[0] = "---myOldValue1a---";
			dr[1] = "---myOldValue1a---";
			dr[2] = "---myOldValue1a---";
			dr[3] = "---myOldValue1a---";
			dr[4] = "---myOldValue1a---";
			dt.Rows.Add(dr);

			dr = dt.NewRow();
			dr[0] = "---myOldValue1b---";
			dr[1] = "---myOldValue1b---";
			dr[2] = "---myOldValue1b---";
			dr[3] = "---myOldValue1b---";
			dr[4] = "---myOldValue1b---";
			dt.Rows.Add(dr);

			dr = dt.NewRow();
			dr[0] = "---myOldValue2---";
			dr[1] = "---myOldValue2---";
			dr[2] = "---myOldValue2---";
			dr[3] = "---myOldValue2---";
			dr[4] = "---myOldValue2---";
			dt.Rows.Add(dr);

			return dt;
		}
	}

	[TestClass]
	public class LargeText : Target.LargeText
	{
		private string[] input = new string[] { "abcdefg", "hijklmn", "opqrstuv", "wxyz1234" };

		[TestMethod]
		[TestCategory("Unit")]
		public void FindLine1Test() 
		{
			for (int x = 0; x < input.Length; x++)
				for (int y = 0; y < input[x].Length; y++)
					Assert.AreEqual(x, Target.LargeText.FindLine(input, input[x][y].ToString()));
		}
		[TestMethod]
		[TestCategory("Unit")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindLine1NullTest1()
		{
			Target.LargeText.FindLine(null, "bleh");
		}
		[TestMethod]
		[TestCategory("Unit")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindLine1NullTest2()
		{
			Target.LargeText.FindLine(input, null);
		}

		[TestMethod]
		[TestCategory("Unit")]
		public void FindLine2Test()
		{
			for (int x = 0; x < input.Length; x++)
				for (int y = 0; y < input[x].Length; y++) {
					Assert.AreEqual(x, Target.LargeText.FindLine(input, input[x][y].ToString(), 0), input[x][y].ToString());
					Assert.AreEqual(x, Target.LargeText.FindLine(input, input[x][y].ToString(), x), input[x][y].ToString());
					Assert.AreEqual(-1, Target.LargeText.FindLine(input, input[x][y].ToString(), x + 1), input[x][y].ToString()); }
		}
		[TestMethod]
		[TestCategory("Unit")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindLine2NullTest1()
		{
			Target.LargeText.FindLine(null, "bleh", 0);
		}
		[TestMethod]
		[TestCategory("Unit")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindLine2NullTest2()
		{
			Target.LargeText.FindLine(input, null, 0);
		}
	}
}
